package com.example.tosco;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

public class HomeActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);
		if (savedInstanceState == null) {
			getFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.home, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {
		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_home, container, false);
			ToggleButton t4 = (ToggleButton) rootView.findViewById(R.id.toggleButton4);
	    	ToggleButton t5 = (ToggleButton) rootView.findViewById(R.id.toggleButton5);
	    	String retorno;
	    	
			Button botao = (Button) rootView.findViewById(R.id.buttonSair);
			
			botao.setOnClickListener(new View.OnClickListener() {
			  public void onClick(View v) {
				  getActivity().finish();
			  }    
			});
			
			ConfigDAO configDao = new ConfigDAO(getActivity());
			configDao.open();
			Config c = configDao.busca(1);
			TextView txtSensor = (TextView) rootView.findViewById(R.id.txtSensor1);
			txtSensor.setText(c.getNome());
			c = configDao.busca(2);
			txtSensor = (TextView) rootView.findViewById(R.id.txtSensor2);
			txtSensor.setText(c.getNome());
			c = configDao.busca(3);
			txtSensor = (TextView) rootView.findViewById(R.id.txtSensor3);
			txtSensor.setText(c.getNome());
			c = configDao.busca(4);
			txtSensor = (TextView) rootView.findViewById(R.id.txtLuz4);
			txtSensor.setText(c.getNome());
			c = configDao.busca(5);
			txtSensor = (TextView) rootView.findViewById(R.id.txtSirene);
			txtSensor.setText(c.getNome());
			configDao.close();
			
	    	WebService ws = new WebService();
	    	ws.atualiza();
			retorno = ws.executar("tosco/lerSensores.php");

			if (!retorno.trim().equals("")){
		    	retorno = retorno.substring(0, retorno.length()-1);
		    	
		    	String[] r = retorno.split("#");
		    	
		    	ToggleButton t1 = (ToggleButton) rootView.findViewById(R.id.toggleButton1);
		    	if (r[0].split(";")[1].equals("1")) {
			    	t1.setChecked(false);
				}else{
			    	t1.setChecked(true);
				}
		    	ToggleButton t2 = (ToggleButton) rootView.findViewById(R.id.toggleButton2);
		    	if (r[1].split(";")[1].equals("1")) {
			    	t2.setChecked(false);
				}else{
			    	t2.setChecked(true);
				}
		    	ToggleButton t3 = (ToggleButton) rootView.findViewById(R.id.toggleButton3);
		    	if (r[2].split(";")[1].equals("1")) {
			    	t3.setChecked(false);
				}else{
			    	t3.setChecked(true);
				}
		    	
		    	if (r[3].split(";")[1].equals("1")) {
			    	t4.setChecked(false);
				}else{
			    	t4.setChecked(true);
				}
		    	if (r[4].split(";")[1].equals("1")) {
			    	t5.setChecked(false);
				}else{
			    	t5.setChecked(true);
				}
			}
			
			t4.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				String retorno;
		    	WebService ws = new WebService();

				@Override
		        public void onCheckedChanged(CompoundButton toggleButton, boolean isChecked) {
		        	if (isChecked){
		    			retorno = ws.executar("tosco/enviarComando.php?comando=H6");
		        	}else{
		    			retorno = ws.executar("tosco/enviarComando.php?comando=L6");
		        	}
		        }
		    });
			
			t5.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				String retorno;
		    	WebService ws = new WebService();

		        @Override
		        public void onCheckedChanged(CompoundButton toggleButton, boolean isChecked) {
		        	if (isChecked){
		    			retorno = ws.executar("tosco/enviarComando.php?comando=H10");
		        	}else{
		    			retorno = ws.executar("tosco/enviarComando.php?comando=L10");
		        	}
		        }
		    });

			return rootView;
		}
	}
}
